package com.cm309.hospital.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cm309.hospital.model.Doctor;
import com.cm309.hospital.model.DoctorSchedule;

public class DoctorScheduleDao {
	public static MyDataSource mds = new MyDataSource();
	
	public List<DoctorSchedule> queryByDoctor(String doctor_id) {
		List<DoctorSchedule> schedule = new ArrayList<DoctorSchedule>();

		try {
			
			Connection conn = mds.getConnection();
			PreparedStatement ps = null;
			try {
				ps = conn.prepareStatement("select doctor_schedule_id,datetime,am_pm_ind from doctor_schedule where doctor_id = ? ");
				ps.setString(1, doctor_id);
				ResultSet rs = ps.executeQuery();

				while (rs.next()) {
					DoctorSchedule c = new DoctorSchedule(rs.getString("doctor_schedule_id"),
							rs.getString("datetime"),
							rs.getString("am_pm_ind"));

					schedule.add(c);
				}

			} finally {
				ps.close();
			}
			return schedule;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	public DoctorSchedule queryByScheduleId(String doctor_schedule_id) {
		DoctorSchedule schedule = new DoctorSchedule();

		try {
			
			Connection conn = mds.getConnection();
			PreparedStatement ps = null;
			try {
				ps = conn.prepareStatement("select doctor_schedule_id,datetime,am_pm_ind from doctor_schedule where doctor_schedule_id = ? ");
				ps.setString(1, doctor_schedule_id);
				ResultSet rs = ps.executeQuery();

				if (rs.next()) {
					schedule = new DoctorSchedule(rs.getString("doctor_schedule_id"),
							rs.getString("datetime"),
							rs.getString("am_pm_ind"));

				}

			} finally {
				ps.close();
			}
//			return schedule;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return schedule;
	}
	
}
